<template>
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="120px" v-show="showSearch">
        <el-form-item label="编号" prop="num">
            <el-input v-model="queryParams.num" placeholder="请输入编号" clearable />
        </el-form-item>
        <el-form-item label="名称" prop="taskName">
            <el-input v-model="queryParams.taskName" placeholder="请输入名称" clearable />
        </el-form-item>
        <el-form-item label="提交人" prop="submitUserName">
            <el-input v-model="queryParams.submitUserName" placeholder="请输入提交人" clearable />
        </el-form-item>
        <el-form-item label="省" prop="provinceId">
            <el-select v-model="queryParams.provinceId" placeholder="请选择省" @change="chooseProvince" clearable>
                <el-option v-for="item in provinceData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="市" prop="cityId">
            <el-select v-model="queryParams.cityId" placeholder="请选择市" @change="chooseCity" clearable>
                <el-option v-for="item in cityData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="区/县" prop="areaId">
            <el-select v-model="queryParams.areaId" placeholder="请选择区/县" @change="chooseArea" clearable>
                <el-option v-for="item in areaData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="handleQuery">搜索</el-button>
            <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
            <el-button type="primary" @click="handleDownloads" :disabled="multiple">
                <svg-icon class-name="download-icon" icon-class="download" /><span style="margin-left:10px">批量下载</span>
            </el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="dataList" stripe row-key="id" tooltip-effect="light">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="编号" align="center" prop="num" />
        <el-table-column label="名称" align="center" prop="taskName" width="255" show-overflow-tooltip />
        <el-table-column label="提交人" align="center" prop="submitUserName" />
        <el-table-column label="报告" align="center" prop="reportName" width="255" show-overflow-tooltip>
            <template #default="scope">
                <el-button type="text" @click="handleDownload(scope.row)">{{ scope.row.reportName }}</el-button>
            </template>
        </el-table-column>
        <el-table-column label="地区" align="center" prop="district" show-overflow-tooltip />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
                <el-button type="text" @click="handlePass(scope.row)">通过</el-button>
                <el-button type="text" @click="handleNoPass(scope.row)">不通过</el-button>
            </template>
        </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
        @pagination="getList" />

    <!-- 审核不通过对话框 -->
    <el-dialog v-model="noPassOpen" v-if="noPassOpen" title="审核不通过" width="900px" append-to-body @close="cancel">
        <p></p>
        <el-form ref="noPassRef" :model="form" :rules="rules" label-width="150px">
          <el-form-item label="审核结果"><span>不通过</span></el-form-item>
          <el-form-item label="不通过原因" prop="rejectAdvice">
            <el-input v-model="form.rejectAdvice" type="textarea" maxlength="500" show-word-limit
              :autosize="{ minRows: 5, maxRows: 5}" placeholder="请输入不通过原因" />
          </el-form-item>
          <el-form-item label="附件" prop="attachmentId">
            <fileUploadNoPass ref="fileUpload" v-model="form.fileList" :data="report"  :limit='1' :isShowTip="false" @uploadFile="uploadFile" @delFile="delFile"></fileUploadNoPass>
        </el-form-item>
      </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="confirmNoPass">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>
  
<script setup>
import { getProvince, getChildArea } from "@/api/system/division";
import { getAllList,pass ,cancelCheck} from "@/api/resultManage/reportReview.js";
import fileUploadNoPass from './FileUpload';
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const loading = ref(false);
const noPassOpen = ref(false);
const total = ref(0);
const showSearch = ref(true);
const report = ref({});
const provinceData = ref([]); //省
const cityData = ref([]); //市
const areaData = ref([]); //区、县

const data = reactive({
    queryParams: {
        page: 1,
        rows: 10,
        provinceId: undefined,
        cityId: undefined,
        areaId: undefined,
        province: undefined,
        city: undefined,
        area: undefined,
        num: undefined,
        taskName: undefined,
        submitUserName:undefined,
    },
    form:{
        rejectAdvice:undefined,
    },
    rules:{
        rejectAdvice: [{ required: true, message: "不通过原因不能为空", trigger: "blur" }],
    }
})
const { queryParams, form, rules } = toRefs(data)


/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.page = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    dataList.value = [];
    proxy.resetForm("queryRef");
    queryParams.value.province = undefined,
    queryParams.value.city = undefined,
    queryParams.value.are = undefined,
    handleQuery();
}

/** 查询数据列表 */
function getList() {
    loading.value = true;
    getAllList(queryParams.value).then(response => {
        dataList.value = response.data.rows ? response.data.rows : [];
        total.value = response.data.total;
        loading.value = false;
    });
}

// 单个下载文件
function handleDownload(row) {
    proxy.download("/reportCheck/downloadFile", {localReport:row.localReport,reportSavePath:row.reportSavePath }, row.reportName);
}

// 通过
function handlePass(row) {
    pass({reportId:row.id,checkStatus:'3',reportType:row.reportType}).then(res=>{
        proxy.$modal.msgSuccess("审核完成");
        getList();
    })
}

// 确定不通过
function confirmNoPass(){
    proxy.$refs["noPassRef"].validate(valid => {
    if (valid) {
        const query = {
            checkStatus:'2',
            reportType:report.value.reportType,
            reportId:report.value.reportId,
            rejectFile:form.value.fileList.length?form.value.fileList[0].fileName:'',
            rejectRealFile:form.value.fileList.length?form.value.fileList[0].realFileName:'',
            rejectAdvice:form.value.rejectAdvice,
        }
        pass(query).then(res=>{
            proxy.$modal.msgSuccess("审核完成");
            noPassOpen.value = false;
            getList();
        })
    }
  });
}
// 取消不通过审核
function cancel(){
    console.log(form.value);
    if(form.value.fileList.length){
        cancelCheck({reportId:report.value.reportId,reportType:report.value.reportType}).then(res=>{
            noPassOpen.value = false;
        })
    }else{
        noPassOpen.value = false;
    }
}
function uploadFile(value){
    form.value.fileList = [value];
}
function delFile(value){
    form.value.fileList = [];
}

// 不通过
function handleNoPass(row) {
    report.value = {
        reportType:row.reportType,
        reportId:row.id,
    }
    noPassOpen.value = true;
    proxy.resetForm("noPassRef");
    form.value = {};
    form.value.fileList = [];
}

/** 获取省列表 */
function getProvinceData() {
    getProvince().then((response) => {
        provinceData.value = response.data;
    });
}

function chooseProvince(value) {
    queryParams.value.city = null;
    queryParams.value.area = null;
    queryParams.value.cityId = null;
    queryParams.value.areaId = null;
    cityData.value = [];
    areaData.value = [];
    getChildArea(value).then(response => {
        cityData.value = response.data;
    });
    queryParams.value.province = provinceData.value.find(item => item.id == value)?.name;
}

function chooseArea(value) {
    queryParams.value.area = areaData.value.find(item => item.id == value)?.name;
}

function chooseCity(value) {
    queryParams.value.area = null;
    queryParams.value.areaId = null;
    areaData.value = [];
    getChildArea(value).then(response => {
        areaData.value = response.data;
    });
    queryParams.value.city = cityData.value.find(item => item.id == value)?.name
}


getProvinceData();
getList();
</script>
  
<style lang="scss" scoped>
.hsdTime {
    margin-right: 0px !important;

}
</style>